﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BeiKe.PhotoTransfer.Winform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            txt_ip.Text = "127.0.0.1";
            txt_port.Text = "9000";


        }
        protected Lib.PhotoTransfer transfer = new Lib.PhotoTransfer("127.0.0.1",9000);
        private async void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += "Running\r\n";

            textBox1.Text += "RESULT:\r\n接受" + (await transfer.StartServe()?"成功":"失败");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog file_dialog = new OpenFileDialog();

            file_dialog.ShowDialog();

            if (String.IsNullOrEmpty(file_dialog.FileName)) return;

            transfer.SendFileAsync(file_dialog.FileName);

            Timer timer = new Timer();

            timer.Tick += setProgress;

            timer.Start();
        }

        private void setProgress(object sender, EventArgs e)
        {
            int progress = (int)(transfer.Progress*100);

            if (progress >= 100)
            {
                progressBar1.Value = 100;
                ((Timer)sender).Stop();
            }
            else
            {
                progressBar1.Value = progress;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            transfer.EndPoint = txt_ip.Text + ":" + txt_port.Text;
        }
    }
}
